var app = getApp()
Page({
  data: {
    /** 
        * 页面配置 
        */
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: "",
    hidden: true,
    callHidden: true,
  },
  onLoad: function (options) {//加载我的页面传过来的id并从后台得到相应的数据
    var that = this;
    this.setData({
      imageUrl: app.globalData.imageUrl
    })
    if (options.id == 0) {
      orderAllDetail(this)
    } else {
      if (options.id == 5) {
        afterSale(this)
      } else {
        requestOrder(this, options.id)
      }

    }
    that.setData({//从我的页面中获取id转换成对应页面
      currentTab: options.id
    })
    // console.log(that.data.orderDetail)
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({//获取滑块的高宽

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  callSuper: function (e) {//催单显示模态框
    // console.log(e.target.dataset.superid)//当前订单的超市id   
    wx.showModal({
      title: '是否拨打催单电话',
      cancelText: '再等一会',
      confirmText: '立即拨打',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.allUrl + "/orderFormAction/reminder",
            data: {
              superID: e.target.dataset.superid
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success               
              wx.makePhoneCall({
                phoneNumber: res.data + "",
              })
            }
          })
        }
      }
    })
  },
  callOther: function (e) {//催单显示模态框
    //console.log(e.target.dataset.otherid)//当前订单的超市id
    wx.showModal({
      title: '是否拨打催单电话',
      cancelText: '再等一会',
      confirmText: '立即拨打',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.allUrl + "/seckillAction/getOtherInfo",
            data: {
              id: e.target.dataset.otherid
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success
              //console.log(res.data)
              wx.makePhoneCall({
                phoneNumber: res.data[0].otherPhone + "",
              })
            }
          })
        }
      }
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '酒吧订位打折兰桂坊九眼桥少陵路',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  callDirect: function (e) {    
    console.log("催单gs2016");
    wx.showModal({
      title: '是否拨打催单电话',
      cancelText: '再等一会',
      confirmText: '立即拨打',
      success: function (res) {
        //console.log("jjjjj"+e.target.dataset.superid);
        //console.log(e.target.dataset.superid);
        if (res.confirm) {
          wx.request({
            // url: app.globalData.allUrl + "/superMarketAction/getSuperInfo",//查询多个商 家电话
            url: app.globalData.allUrl + "/directAction/getDirectPhone",//查询单个商家电话
            data: {
              directId: e.target.dataset.superid
            },
            /*    data: {//查询多个商家电话
                  superId: e.target.dataset.superid
                },*/
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success
              console.log("ja");
              wx.makePhoneCall({
                phoneNumber: res.data + "",
              })
            }
          })
        }
      }
    })
  },
  toValues: function (e) {//跳转到评价的界面
    wx.navigateTo({
      url: '../value/value?id=' + e.target.dataset.id + '&&typeid=' + e.target.dataset.typeid,
    })
  },

  Complaints: function (e) {//售后投诉订单
    // console.log(e.target.dataset.id)
    app.complaintProductId = e.target.dataset.id;
    wx.showModal({
      title: '是否投诉订单',
      content: '',
      cancelText: '再等等',
      confirmText: '拨打电话',

      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '028-8357-0789',
          })
        }
      }
    })
    // wx.navigateTo({
    //   url: '../Complaints/Complaints',
    //   success: function (res) {
    //     // success
    //   }
    // })
  },

  cancelOrder: function (e) {//在待发货里取消订单
    var that = this;
    wx.showModal({
      title: '是否取消订单',
      content: '',
      cancelText: '再考虑',
      confirmText: '确认取消',
      success: function (res) {
        if (res.confirm) {
          if (e.target.dataset.paymenttype == 1) {
            sendCancelOrderToBack(that, e.target.dataset.id);
          } else if (e.target.dataset.paymenttype == 2) {
            sendCancelOnlineToBack(that, e.target.dataset.orderformid)
          }
          // that.setData({
          //   orderDetail: ""
          // })
          // requestOrder(that, 1)
        }
      }
    })
  },
  aLLCancelOrder: function (e) {//在全部里取消订单
    var that = this;
    wx.showModal({
      title: '是否取消订单',
      content: '',
      cancelText: '再考虑',
      confirmText: '确认取消',
      success: function (res) {
        if (res.confirm) {
          // console.log(112)
          if (e.target.dataset.paymenttype == 1) {
            sendCancelOrderToBack(that, e.target.dataset.id);
          } else {
            sendCancelOnlineToBack(that, e.target.dataset.orderformid)
          }
          that.setData({
            orderAllDetailreceipt: "",
            orderAllDetailShip: "",
            orderAllDetailvalue: ""
          })
          orderAllDetail(that)
        }
      }
    })
  },
  allSureReceipt: function (e) {//全部里确认收货
    var Id = e.target.dataset.order;
    sureReceiptAll(this, Id);//先把確認收货发送到后台

  },
  // cancel: function () {//点击再考虑下后让模态框小时
  //   this.setData({
  //     hidden: true
  //   })
  // },
  // confirm: function () {//点击确认取消让模态框小时并且将订单信息发送到后台
  //   var that = this;
  //   this.setData({
  //     hidden: true
  //   })
  //   sendCancelOrderToBack(that,)
  // },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  deleteOrder: function (e) {//删除售后订单
    var that = this;
    wx.request({
      url: app.globalData.allUrl + "/orderFormAction/cancelOrderForm",
      data: {
        id: e.target.dataset.id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        that.setData({
          orderSolved: "",
          orderSolving: "",
          orderComplainted: "",
        })
        afterSale(that)
      }
    })
  },
  deleteOrderComplete: function (e) {//删除已完成订单
    var that = this;
    wx.request({
      url: app.globalData.allUrl + "/orderFormAction/cancelOrderForm",
      data: {
        id: e.target.dataset.id
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        that.setData({
          orderDetail: ""
        })
        requestOrder(that, 3)
      }
    })
  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
      if (e.target.dataset.current == 0) {
        orderAllDetail(this)
      } else {
        if (e.target.dataset.current == 5) {
          afterSale(this)
        } else {
          requestOrder(this, e.target.dataset.current)
        }

      }

    }
  },
  onShow: function () {
    if (app.returnOrderFormDetailNumber == 2 && this.data.currentTab == 0) {//在退货原因页面，如果退货成功就将returnOrderFormDetailNumber的值设为2，进入这个页面以后，就可以根据这个值来判断是否要刷新页面请求新的订单
      this.setData({
        orderAllDetailreceipt: "",
        orderAllDetailShip: "",
        orderAllDetailvalue: ""
      })
      orderAllDetail(this)
      this.setData({
        orderDetail: app.returnOrderFormDetail
      })
    } else if (app.complaintOrderFormDetailNumber == 5) {
      this.setData({
        orderSolved: "",
        orderSolving: "",
        orderComplainted: ""
      })
      afterSale(this)

    } else if (app.returnOrderFormDetailNumber == 2 && this.data.currentTab == 2) {
      requestOrder(this, 2)
    }
  },
  returnProduct: function (e) {//点击退货

    var time = Date.now();
    var d = new Date(e.target.dataset.oldtime);
    var oldtime = d.getTime();
    var timec = time - oldtime
    var temp = 3600 * 1000;
   /* if (timec <= temp) {
      wx.showToast({
        title: "下单后满一小时才可退货",
        icon: "loading"
      })
    } else {*/
      app.returenProductId = e.target.dataset.id
      app.returenProductorderformid = e.target.dataset.orderformid;
      app.paymentstypes = e.target.dataset.paymenttype;
      app.returnorderformids = e.target.dataset.orderformid;
      wx.navigateTo({
        url: '../returnProduct/returnProduct'
      })
  //  }

  },
  logistic: function (e) {//跳转到查询物流页面
    wx.navigateTo({
      url: '/pages/logistics/logistics?id=' + e.target.dataset.logistic,
    })
    // console.log(e.target.dataset.logistic)
  },
  returnGroupProduct: function (e) {//取消团购订单
    //console.log(e.target.dataset.id)
    var that = this;
    wx.showModal({
      title: '是否取消订单',
      content: '',
      cancelText: '再考虑',
      confirmText: '确认取消',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.allUrl + "/refundAction/transferPay",
            data: {
              TransferNo: e.target.dataset.id,
              // userId:wx.getStorageSync('userid')
            },
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            // header: {}, // 设置请求的 header
            success: function (res) {
              // success
              wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000
              })
              that.setData({
                orderDetail: ""
              })
              requestOrder(that, 2)
            }
          })
        }
      }
    })
  },

  // returnProducts: function (e) {//点击退货
  //   // console.log(e.target.dataset.orderformid)
  //   // console.log(e.target.dataset.id)
  //   app.returenProductId = e.target.dataset.id
  //   app.returenProductorderformid = e.target.dataset.orderformid;
  //    wx.showModal({
  //     title: '是否取消订单',
  //     content: '',
  //     cancelText: '再考虑',
  //     confirmText: '确认取消',
  //     success: function (res) {
  //       if (res.confirm) {
  //         // console.log(112)
  //         sendCancelOrderToBack(that, e.target.dataset.id);
  //         that.setData({
  //           orderAllDetailreceipt: "",
  //           orderAllDetailShip: "",
  //           orderAllDetailvalue: ""
  //         })
  //         orderAllDetail(that)
  //       }
  //     }
  //   })
  //   wx.navigateTo({
  //     url: '../returnProduct/returnProduct',
  //     success: function (res) {
  //       // success
  //     },
  //     fail: function (res) {
  //       // fail
  //     },
  //     complete: function (res) {
  //       // complete
  //     }
  //   })

  // }


  // ，
  sureReceipt: function (e) {//确认收货
    var that = this;
    // console.log(e.target.dataset.order)确认收货的订单id
    var Id = e.target.dataset.order;
    sureReceiptAll(this, Id);

  }
})

function requestOrder(that, orderID) {//从后台获取当前状态所需订单详情页面
  that.setData({
    orderDetail: "",
  })
  wx.request({
    url: app.globalData.allUrl + "/orderFormAction/getOrderFormByStatus",
    data: {
      userid: wx.getStorageSync('userid'),//用户唯一ID
      status: orderID//订单状态ID（1-5）
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
       console.log(res.data)
      var orderDetail = res.data//得到大订单
      for (var types in orderDetail) {
        var typeName;
        if (orderDetail[types].typeId == 0) {
          typeName = "秒杀";
          orderDetail[types].typeName = typeName;
          orderDetail[types].imgsrc = "seckills"
        } else if (orderDetail[types].typeId == 1) {
          typeName = "超市";
          orderDetail[types].typeName = typeName;
          orderDetail[types].imgsrc = "goods"
        } else if (orderDetail[types].typeId == 2) {
          typeName = "团购";
          orderDetail[types].typeName = typeName;
          orderDetail[types].imgsrc = "seckills"
        } else if (orderDetail[types].typeId == 3) {
          typeName = "直营";
          orderDetail[types].typeName = typeName;
          orderDetail[types].imgsrc = "directs"
        }
      }
      that.setData({
        orderDetail: orderDetail
      })
      // console.log(res.data)//从后台根据用户id与订单状态来获取对应的订单数据
    }
  })

}

function afterSale(that) {//从后台获取售后订单
  wx.request({
    url: app.globalData.allUrl + "/orderFormAction/getOrderFormByStatus",
    data: {
      userid: wx.getStorageSync('userid'),//用户唯一ID
      status: 5 //订单状态ID（1-5）
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
      var orderDetail = res.data//得到大订单
      var orderSolved = [];
      var orderSolving = [];
      var orderComplainted = [];
      for (var statu in orderDetail) {
        var typeName;
        if (orderDetail[statu].typeId == 0) {
          typeName = "秒杀";
          orderDetail[statu].typeName = typeName
          orderDetail[statu].imgsrc = "seckills"
        } else if (orderDetail[statu].typeId == 1) {
          typeName = "超市";
          orderDetail[statu].typeName = typeName
          orderDetail[statu].imgsrc = "goods"
        } else if (orderDetail[statu].typeId == 2) {
          typeName = "团购";
          orderDetail[statu].typeName = typeName
          orderDetail[statu].imgsrc = "seckills"
        } else if (orderDetail[statu].typeId == 3) {
          typeName = "直营";
          orderDetail[statu].typeName = typeName
          orderDetail[statu].imgsrc = "directs"
        }
        if (orderDetail[statu].status == 6) {//如果订单状态为6
          orderSolved.unshift(orderDetail[statu])//则添加到已解决的售后订单中         
          that.setData({
            orderSolved: orderSolved
          })
        } else if (orderDetail[statu].status == 5) {//如果订单状态为5
          orderSolving.unshift(orderDetail[statu])//则添加到解决中的售后订单中去
          that.setData({
            orderSolving: orderSolving
          })
          console.log("jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj");
          console.log(that.data.orderSolving);
        } else if (orderDetail[statu].status == 7) {//如果订单状态为7
          orderComplainted.unshift(orderDetail[statu])//则添加到已投诉的售后订单中去
          that.setData({
            orderComplainted: orderComplainted
          })
        }
      }
      // console.log(res.data)//从后台根据用户id与订单状态来获取对应的订单数据
    }
  })
}

function orderAllDetail(that) {//请求全部订单详情
  wx.request({
    url: app.globalData.allUrl + "/orderFormAction/getOrderFormByUserID",
    data: {
      userID: wx.getStorageSync('userid')
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      var orderAllDetail = res.data//得到大订单
      var orderAllDetailShip = [];
      var orderAllDetailreceipt = [];
      var orderAllDetailvalue = [];
      for (var status in orderAllDetail) {
        var typeName;
        if (orderAllDetail[status].typeId == 0) {
          typeName = "秒杀";
          orderAllDetail[status].typeName = typeName;
          orderAllDetail[status].imgsrc = "seckills"
        } else if (orderAllDetail[status].typeId == 1) {
          typeName = "超市";
          orderAllDetail[status].typeName = typeName;
          orderAllDetail[status].imgsrc = "goods"
        } else if (orderAllDetail[status].typeId == 2) {
          typeName = "团购";
          orderAllDetail[status].typeName = typeName;
          orderAllDetail[status].imgsrc = "seckills"
        } else if (orderAllDetail[status].typeId == 3) {
          typeName = "直营";
          orderAllDetail[status].typeName = typeName;
          orderAllDetail[status].imgsrc = "directs"
        }
        if (orderAllDetail[status].status == 1) {//如果订单状态为一
          orderAllDetailShip.unshift(orderAllDetail[status])//则添加到待发货订单中
          that.setData({
            orderAllDetailShip: orderAllDetailShip
          })
        } else if (orderAllDetail[status].status == 2) {//二，待收货
          orderAllDetailreceipt.unshift(orderAllDetail[status])
          that.setData({
            orderAllDetailreceipt: orderAllDetailreceipt
          })
        } else if (orderAllDetail[status].status == 3) {

          orderAllDetailvalue.unshift(orderAllDetail[status])
          that.setData({
            orderAllDetailvalue: orderAllDetailvalue
          })
        }
      }
    }
  })
}


function sureReceiptAll(that, Id) {//确认收货
  wx.request({
    url: app.globalData.allUrl + "/orderFormAction/takeDeliveryOfGoods",
    data: {
      userID: wx.getStorageSync('userid'),
      id: Id
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      if (res.data) {
        wx.showToast({
          title: '确认收货成功',
          icon: 'success',
          duration: 2000
        })
        that.setData({
          orderDetail: "",
          orderAllDetailreceipt: "",
          orderAllDetailShip: "",
          orderAllDetailvalue: ""
        })
        requestOrder(that, 2)//再次加载待收货订单
        orderAllDetail(that)  //再从新加载一次全部订单
      }
    }
  })
}

function sendCancelOrderToBack(that, Id, callback) {//将取消订单的信息发送至后台
  wx.request({
    url: app.globalData.allUrl + "/orderFormAction/cancelOrderForm",
    data: {
      id: Id
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
      //  callback;
      wx.showToast({
        title: '取消订单成功',
        icon: 'success',
        duration: 2000
      })
      that.setData({
        orderDetail: ""
      })
      requestOrder(that, 1)
    }
  })

}

function sendCancelOnlineToBack(that, id) {//将取消在线支付订单的信息发送至后台
  wx.request({
    url: app.globalData.allUrl + "/refundAction/transferPay",
    data: {
      TransferNo: id,
      // userId:wx.getStorageSync('userid')
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
      wx.showToast({
        title: res.data.msg,
        icon: 'success',
        duration: 2000
      })
      that.setData({
        orderDetail: "",
        orderAllDetailreceipt: "",
        orderAllDetailShip: "",
        orderAllDetailvalue: ""
      })
      requestOrder(that, 1)
    }
  })
}
